﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class LePhiTruocBaDatBO : Csla.BusinessBase<LePhiTruocBaDatBO>
    {
        #region Business Properties

        private int? _LePhiTruocBaDatID = null;
        public int? CL_LePhiTruocBaDatID
        {
            get { return this._LePhiTruocBaDatID; }
            set { this._LePhiTruocBaDatID = value; PropertyHasChanged("CL_LePhiTruocBaDatID"); }
        }

        private int? _HoSoID = Session.KTTT_HoSoID;
        public int? CL_HoSoID
        {
            get { return this._HoSoID; }
            set { this._HoSoID = value; PropertyHasChanged("CL_HoSoID"); }
        }

        private int? _LoaiDienTichID = null;
        public int? CL_LoaiDienTichID
        {
            get { return this._LoaiDienTichID; }
            set { this._LoaiDienTichID = value; PropertyHasChanged("CL_LoaiDienTichID"); }
        }

        private string _LoaiDienTichID_Text = null;
        public string CL_LoaiDienTichID_Text
        {
            get { return this._LoaiDienTichID_Text; }
            set { this._LoaiDienTichID_Text = value; PropertyHasChanged("CL_LoaiDienTichID_Text"); }
        }

        private double? _LoaiDienTichID_Value = 0;
        public double? CL_LoaiDienTichID_Value
        {
            get { return this._LoaiDienTichID_Value; }
            set { this._LoaiDienTichID_Value = value; PropertyHasChanged("CL_LoaiDienTichID_Value"); }
        }

        private double? _DienTich = 0;
        public double? CL_DienTich
        {
            get { return this._DienTich; }
            set { this._DienTich = value; PropertyHasChanged("CL_DienTich"); }
        }

        private double? _GiaTinhThu1M2 = Session.KTTT_GiaChuan1M2;
        public double? CL_GiaTinhThu1M2
        {
            get { return this._GiaTinhThu1M2; }
            set { this._GiaTinhThu1M2 = value; PropertyHasChanged("CL_GiaTinhThu1M2"); }
        }

        private double? _TyLe = 0.5;
        public double? CL_TyLe
        {
            get { return this._TyLe; }
            set { this._TyLe = value; PropertyHasChanged("CL_TyLe"); }
        }

        private double? _PhaiNop = 0;
        public double? CL_PhaiNop
        {
            get { return this._PhaiNop; }
            set { this._PhaiNop = value; PropertyHasChanged("CL_PhaiNop"); }
        }

        private double? _HeSoKhac = 1;
        public double? CL_HeSoKhac
        {
            get { return this._HeSoKhac; }
            set { this._HeSoKhac = value; PropertyHasChanged("CL_HeSoKhac"); }
        }

        private bool? _TruyThu = false;
        public bool? CL_TruyThu
        {
            get { return this._TruyThu; }
            set { this._TruyThu = value; PropertyHasChanged("CL_TruyThu"); }
        }

        private string _GhiChu = null;
        public string CL_GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("CL_GhiChu"); }
        }

        private int? _USRC = Session.LoginID;
        public int? CL_USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("CL_USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? CL_DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("CL_DATC"); }
        }

        private int? _USRM = null;
        public int? CL_USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("CL_USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? CL_DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("CL_DATM"); }
        }

        private bool? _DELE = false;
        public bool? CL_DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("CL_DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _LePhiTruocBaDatID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newLePhiTruocBaDatID = -1;
        private LePhiTruocBaDatBO()
        {
            _LePhiTruocBaDatID = newLePhiTruocBaDatID--;
        }

        public LePhiTruocBaDatBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public LePhiTruocBaDatBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static LePhiTruocBaDatBO NewRootLePhiTruocBaDatBO()
        {
            return DataPortal.Create<LePhiTruocBaDatBO>(new RootCriteria());
        }

        internal static LePhiTruocBaDatBO NewChildLePhiTruocBaDatBO()
        {
            return DataPortal.Create<LePhiTruocBaDatBO>(new ChildCriteria());
        }

        public static LePhiTruocBaDatBO GetRootLePhiTruocBaDatBO(int _LePhiTruocBaDatID)
        {
            return DataPortal.Fetch<LePhiTruocBaDatBO>(new RootCriteria(_LePhiTruocBaDatID));
        }

        public static LePhiTruocBaDatBO GetRootLePhiTruocBaDatBOByWhere(string where)
        {
            return DataPortal.Fetch<LePhiTruocBaDatBO>(where);
        }

        internal static LePhiTruocBaDatBO GetChildLePhiTruocBaDatBO(SqlDataReader dataReader)
        {
            return new LePhiTruocBaDatBO(dataReader);
        }

        public static void DeleteLePhiTruocBaDatBO(int _LePhiTruocBaDatID)
        {
            DataPortal.Delete(new RootCriteria(_LePhiTruocBaDatID));
        }

        public static void DeleteLePhiTruocBaDatBO(int _LePhiTruocBaDatID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_LePhiTruocBaDatID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int LePhiTruocBaDatID;
            public bool directDelete = false;
            public RootCriteria(int _LePhiTruocBaDatID)
            {
                this.LePhiTruocBaDatID = _LePhiTruocBaDatID;
                this.directDelete = false;
            }

            public RootCriteria(int _LePhiTruocBaDatID, bool directDelete)
            {
                this.LePhiTruocBaDatID = _LePhiTruocBaDatID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int LePhiTruocBaDatID;
            public bool directDelete = false;
            public ChildCriteria(int _LePhiTruocBaDatID)
            {
                this.LePhiTruocBaDatID = _LePhiTruocBaDatID;
                this.directDelete = false;
            }

            public ChildCriteria(int _LePhiTruocBaDatID, bool directDelete)
            {
                this.LePhiTruocBaDatID = _LePhiTruocBaDatID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LePhiTruocBaDatBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[LePhiTruocBaDatID] = " + criteria.LePhiTruocBaDatID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LePhiTruocBaDatBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["LePhiTruocBaDatID"] is DBNull))
                this._LePhiTruocBaDatID = (int)dataReader["LePhiTruocBaDatID"];
            if (!(dataReader["HoSoID"] is DBNull))
                this._HoSoID = (int)dataReader["HoSoID"];
            if (!(dataReader["LoaiDienTichID"] is DBNull))
                this._LoaiDienTichID = (int)dataReader["LoaiDienTichID"];
            if (!(dataReader["LoaiDienTichID_Text"] is DBNull))
                this._LoaiDienTichID_Text = (string)dataReader["LoaiDienTichID_Text"];
            if (!(dataReader["LoaiDienTichID_Value"] is DBNull))
                this._LoaiDienTichID_Value = (double)dataReader["LoaiDienTichID_Value"];
            if (!(dataReader["DienTich"] is DBNull))
                this._DienTich = (double)dataReader["DienTich"];
            if (!(dataReader["GiaTinhThu1M2"] is DBNull))
                this._GiaTinhThu1M2 = (double)dataReader["GiaTinhThu1M2"];
            if (!(dataReader["TyLe"] is DBNull))
                this._TyLe = (double)dataReader["TyLe"];
            if (!(dataReader["PhaiNop"] is DBNull))
                this._PhaiNop = (double)dataReader["PhaiNop"];
            if (!(dataReader["HeSoKhac"] is DBNull))
                this._HeSoKhac = (double)dataReader["HeSoKhac"];
            if (!(dataReader["TruyThu"] is DBNull))
                this._TruyThu = (bool)dataReader["TruyThu"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LePhiTruocBaDatBO_InsertBO");
            db.AddInParameter(cmd, "@HoSoID", SqlDbType.Int, this._HoSoID);
            db.AddInParameter(cmd, "@LoaiDienTichID", SqlDbType.Int, this._LoaiDienTichID);
            db.AddInParameter(cmd, "@LoaiDienTichID_Text", SqlDbType.NVarChar, this._LoaiDienTichID_Text);
            db.AddInParameter(cmd, "@LoaiDienTichID_Value", SqlDbType.Float, this._LoaiDienTichID_Value);
            db.AddInParameter(cmd, "@DienTich", SqlDbType.Float, this._DienTich);
            db.AddInParameter(cmd, "@GiaTinhThu1M2", SqlDbType.Float, this._GiaTinhThu1M2);
            db.AddInParameter(cmd, "@TyLe", SqlDbType.Float, this._TyLe);
            db.AddInParameter(cmd, "@PhaiNop", SqlDbType.Float, this._PhaiNop);
            db.AddInParameter(cmd, "@HeSoKhac", SqlDbType.Float, this._HeSoKhac);
            db.AddInParameter(cmd, "@TruyThu", SqlDbType.Bit, this._TruyThu);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewLePhiTruocBaDatID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._LePhiTruocBaDatID = (int)cmd.Parameters["@NewLePhiTruocBaDatID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LePhiTruocBaDatBO_UpdateBO");
            db.AddInParameter(cmd, "@LePhiTruocBaDatID", SqlDbType.Int, this._LePhiTruocBaDatID);
            db.AddInParameter(cmd, "@HoSoID", SqlDbType.Int, this._HoSoID);
            db.AddInParameter(cmd, "@LoaiDienTichID", SqlDbType.Int, this._LoaiDienTichID);
            db.AddInParameter(cmd, "@LoaiDienTichID_Text", SqlDbType.NVarChar, this._LoaiDienTichID_Text);
            db.AddInParameter(cmd, "@LoaiDienTichID_Value", SqlDbType.Float, this._LoaiDienTichID_Value);
            db.AddInParameter(cmd, "@DienTich", SqlDbType.Float, this._DienTich);
            db.AddInParameter(cmd, "@GiaTinhThu1M2", SqlDbType.Float, this._GiaTinhThu1M2);
            db.AddInParameter(cmd, "@TyLe", SqlDbType.Float, this._TyLe);
            db.AddInParameter(cmd, "@PhaiNop", SqlDbType.Float, this._PhaiNop);
            db.AddInParameter(cmd, "@HeSoKhac", SqlDbType.Float, this._HeSoKhac);
            db.AddInParameter(cmd, "@TruyThu", SqlDbType.Bit, this._TruyThu);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_LePhiTruocBaDatID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LePhiTruocBaDatBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@LePhiTruocBaDatID", SqlDbType.Int, criteria.LePhiTruocBaDatID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_LePhiTruocBaDatID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
